package flc.ast.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyDocBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class SelDocAdapter extends StkProviderMultiAdapter<MyDocBean> {

    public SelDocAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(82));
        addItemProvider(new SelDocProvider());
    }

    private boolean isEdit = false;

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private class SelDocProvider extends BaseItemProvider<MyDocBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_doc;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyDocBean bean) {
            baseViewHolder.setText(R.id.tvSelDocItemName, bean.getName());
            baseViewHolder.setText(R.id.tvSelDocItemDate, bean.getDate() + "  " + bean.getSize());
            if (bean.isSelect()) {
                baseViewHolder.setImageResource(R.id.ivSelDocItemSel, R.drawable.axuanz);
            } else {
                baseViewHolder.setImageResource(R.id.ivSelDocItemSel, R.drawable.aweixuanzhong);
            }
            if (isEdit) {
                baseViewHolder.getView(R.id.ivSelDocItemSel).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.ivSelDocItemSel).setVisibility(View.GONE);
            }
            baseViewHolder.setImageResource(R.id.ivSelDocItemImg, getImgIcon(bean.getPath()));
        }
    }

    //根据后缀获取图标
    private int getImgIcon(String path) {
        String suffix = path.substring(path.lastIndexOf("."));
        int icon;
        if (suffix.equals(".pdf") || suffix.equals(".PDF")) {
            icon = R.drawable.apdf;
        } else if (suffix.equals(".ppt") || suffix.equals(".pptx")) {
            icon = R.drawable.appt;
        } else if (suffix.equals(".txt") || suffix.equals(".TXT")) {
            icon = R.drawable.atxt;
        } else if (suffix.equals(".doc") || suffix.equals(".docx")) {
            icon = R.drawable.adoc;
        } else if (suffix.equals(".xls") || suffix.equals(".xlsx")) {
            icon = R.drawable.axcl;
        } else {
            icon = R.drawable.adoc;
        }
        return icon;
    }

}
